jQuery(document).ready(function() {
	if(jQuery('#google_map_add_form').length){
		var lat = ( jQuery('#lat').val() ) ? jQuery('#lat').val() : 36;
		var lng = ( jQuery('#long').val() ) ? jQuery('#long').val() : 138;
		initialize(lat , lng);
	}
});

function setTopstand($id){
	jQuery.post(BASE_URI+'admin/dataman/content_set_top_stand',{id:$id},function(res){
		var url = window.location.href;
		window.location = url;
	});
}




var map;
var marker;
var count=0;

function initialize(lat , lng) {
    var latlng = new google.maps.LatLng(lat,lng);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("google_map_add_form"), myOptions);
    google.maps.event.addListener(map, 'click', function(event) {
    	mapEditor(event.latLng);
    });
    marker = new google.maps.Marker({
		  position: latlng,
		  map: map
	  });
 }

function mapEditor(location) {
    marker.setPosition(location);
    jQuery('#lat').val( location.lat() ); 
    jQuery('#long').val( location.lng() ); 
}

function selectCities(value){
	jQuery.post( BASE_URI+'ranking/aj_get_cities' , {region_id:value} , function(response){
		jQuery('#city_').html(response);
	} );
}


function submit_form_add_item(){
	obj = eval( jQuery('#edit').serializeArray() );
	json = {};
	for (i in obj ) {
	    json[obj[i].name] = obj[i].value
	} 
	
	var error = 0;
	jQuery("input").each(function(){
		if(this.required && this.value ==''){
			jQuery(this).addClass("error"); error ++;
		}else{
			jQuery(this).removeClass("error");
		} 
		
	}); 
/*	var iRegion = parseInt( json.region ); 
	var iCity = parseInt ( json.city );
	if(iRegion * iCity == 0){
		error ++;
		if(iRegion == 0)
			jQuery('#region').addClass("error");
		else
			jQuery('#region').removeClass("error");
		
		if(iCity == 0)
			jQuery('#city').addClass("error");
		else
			jQuery('#city').removeClass("error");
	}else{
		jQuery('#city').removeClass("error");
		jQuery('#region').removeClass("error");
	}
*/	
	var count = 0;
	jQuery('input[rel="select_image"]').each(function(){
		isChecked = ( jQuery(this).attr('checked') ) ? true : false;
		if(isChecked) {
			count++;
			jQuery('#selected_image_err').removeClass("error"); 
			jQuery('#selected_image_err').addClass("hidden");
		}
	});
	 
	if( 0 == count ){
		jQuery('#selected_image_err').removeClass("hidden"); 
		jQuery('#selected_image_err').addClass("error");
		error ++;
	}else{
		jQuery('#selected_image_err').removeClass("error"); 
		jQuery('#selected_image_err').addClass("hidden");
	}
	
	if(!isEmpty(json.user_url)){
		if(!isUrl( json.user_url )){
			jQuery('#user_url').addClass('error');
			error ++;
		}else{
			jQuery('#user_url').removeClass('error');
		}
	}
	
	if( isEmpty( json.image_uploaded_1 ) && isEmpty( json.image_uploaded_2 ) && isEmpty( json.image_uploaded_3 )){
		error ++;
		jQuery('#image_upload_stt1').children().removeClass('hidden');
		jQuery('#image_upload_stt1').children().addClass('error');
	}else{
		jQuery('#image_upload_stt1').children().removeClass('error');
		jQuery('#image_upload_stt1').children().addClass('hidden');
	}
/*	
	if(isEmpty( json.image_uploaded_2 )){
		error ++;
		jQuery('#image_upload_stt2').children().removeClass('hidden');
		jQuery('#image_upload_stt2').children().addClass('error');
	}else{
		jQuery('#image_upload_stt2').children().removeClass('error');
		jQuery('#image_upload_stt2').children().addClass('hidden');
	}
	
	if(isEmpty( json.image_uploaded_3 )){
		error ++;
		jQuery('#image_upload_stt3').children().removeClass('hidden');
		jQuery('#image_upload_stt3').children().addClass('error');
	}else{
		jQuery('#image_upload_stt3').children().removeClass('error');
		jQuery('#image_upload_stt3').children().addClass('hidden');
	}
*/	 
	if(error == 0){
		jQuery('#check_duplicate_name').removeClass("hidden");
		jQuery.post( BASE_URI +'admin/dataman/aj_save_content',{data:obj},function(res){
					window.location= res;
				});
	/*			
		jQuery.post( BASE_URI +'admin/dataman/aj_check_duplicate_name' ,{name:json.name_item,'item_id':json.item_id } , function(response){
			if(response == 'ok'){
				jQuery('#check_duplicate_name_error').addClass("hidden");
				
				jQuery.post( BASE_URI +'admin/dataman/aj_save_content',{data:obj},function(res){
					window.location= res;
				});
			}else{
				jQuery('#check_duplicate_name').addClass("hidden");
				jQuery('#check_duplicate_name_error').removeClass("hidden");
				scroll(0,0);
				return false;
			}
		} );
	*/	
	}else{
		return false;
	}
	
}

function select_image(me, select_pos){
	var isChecked = (jQuery(me).attr('checked')) ? true :false;
	jQuery('input[rel="select_image"]').each(function(){
		jQuery(this).attr('checked',false);
	});
	if(isChecked){
		jQuery(me).attr('checked',true);
	}
}


function fileUpload(form, action_url, div_id, div_preview, upload_frame_id)
{
	 var mainForm = document.edit.action;
	// Create the iframe...
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id",upload_frame_id);
	iframe.setAttribute("name",upload_frame_id);
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	
	// Add to document...
	form.parentNode.appendChild(iframe);
	window.frames[upload_frame_id].name=upload_frame_id;
	
	iframeId = document.getElementById(upload_frame_id);
	
	// Add event...
	var eventHandler = function()  {
		if (iframeId.detachEvent)
			iframeId.detachEvent("onload", eventHandler);
		else
			iframeId.removeEventListener("load", eventHandler, false);
		
		// Message from server...
		if (iframeId.contentDocument) {
			content = iframeId.contentDocument.body.innerHTML;
		} else if (iframeId.contentWindow) {
			content = iframeId.contentWindow.document.body.innerHTML;
		} else if (iframeId.document) {
			content = iframeId.document.body.innerHTML;
		}
		
		arr = content.split('SEP_CHAR'); 
		if(arr[0] != 'error'){
		 	document.getElementById(div_preview).innerHTML = arr[1]; 
			document.getElementById(div_id).innerHTML = '';
			document.getElementById(arr[2]).value= arr[3];
		}	
		else{
			document.getElementById(div_id).innerHTML =  arr[1];
			document.getElementById(arr[2]).value= arr[3];
			
			form.value = '';
		}
			
		// Del the iframe...
		setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
		
		//Reset properties of form...
		form.setAttribute("target",'');
		form.setAttribute("action",mainForm);
		form.setAttribute("method","post");
		form.setAttribute("enctype","");
		form.setAttribute("encoding","");
	}
	
	if (iframeId.addEventListener)
		iframeId.addEventListener("load", eventHandler, true);
	if (iframeId.attachEvent)
		iframeId.attachEvent("onload", eventHandler);
	
	// Set properties of form...
	form.setAttribute("target",upload_frame_id);
	form.setAttribute("action", BASE_URI+'admin/dataman/'+ action_url);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	
	// Submit the form...
	form.submit();
	
	document.getElementById(div_id).innerHTML = sessionStorage.uploading;
}
